import { ref, watch } from 'vue'
import { Form } from 'ant-design-vue'
/**
 * @name: useForm
 * @description:
 *
 */
export default function (formState, rulesState) {
  const useForm = Form.useForm
  const isCompleted = ref(false)
  const { resetFields, validate, clearValidate, validateInfos } = useForm(formState, rulesState, { deep: true })
  const requiredFields = Object.keys(rulesState.value).filter((key) => rulesState.value[key].some((rule) => rule.required))
  watch(
    formState,
    (val) => {
      isCompleted.value = requiredFields.every((key) => val[key] || val[key] === 0)
    },
    { deep: true }
  )
  return { isCompleted, resetFields, clearValidate, validate, validateInfos }
}
